package com.zt.user.web;


import com.zt.user.config.PatternProperties;
import com.zt.user.pojo.User;
import com.zt.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@RequestMapping("/user")
//@RefreshScope
public class UserController {

    @Autowired
    private UserService userService;

    @Resource
    private PatternProperties patternProperties;

    @GetMapping("/now")
    public String now() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(patternProperties.getDateformat()));
    }

    @GetMapping("/prop")
    public PatternProperties prop() {
        return patternProperties;
    }


//    @Value("${pattern.dateformat}")
//    private String dateformat;

//    @GetMapping("/now")
//    public String now() {
//        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat));
//    }


    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id) throws InterruptedException {
        if (id == 1) {
            Thread.sleep(60);
        } else if (id == 2) {
            throw new RuntimeException("id=2,模拟异常");
        }
        return userService.queryById(id);
    }
}
